<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/3
 * Time: 15:17
 */

namespace app\sports_admin\model;


use think\Model;
use think\db;
use app\sports_admin\model\Customer;
use app\sports_admin\model\Region;

class CustomerSchool extends Model
{
    protected $table = "customer_school";
    protected $pk    = "id";
    //关联代理人表
    public function getCustomer()
    {
        return $this->belongsTo("customer","customer_id","id")->field("id,name");
    }
    //关联地区表 省份
    public function getRegion()
    {
        return $this->belongsTo("region","province","id")->field("id,region_name");
    }
    //关联地区表 市
    public function getRegionCity()
    {
        return $this->belongsTo("region","city","id")->field("id,region_name");
    }
    //学校业务信息
    public static function getSchoolService($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::with('getCustomer,getRegion')
            ->where(function($query)use($searchName){
                if($searchName){
                    $query->where('name','like',"%{$searchName}%");
                }})
            ->where('state',0)
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::with('getCustomer,getRegion')
            ->where(function($query)use($searchName){
                if($searchName){
                    $query->where('name','like',"%{$searchName}%");
                }})
            ->where('state',0)
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
}